/**
 *
 * @file tsar.h
 *
 * @brief Header file for program's core
 * 
 * $Id: tsar.h 125 2009-01-15 09:50:24Z henri.doreau $
 */

/*
 * This file is part of Tsar.
 *
 * Tsar is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tsar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tsar.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef TSAR_H
#define TSAR_H

/* -- S T A N D A R D  I N C L U D E S -- */
#include <time.h>
#include <unistd.h>

/* -- O T H E R  I N C L U D E S -- */
#include <output.h>
#include <options.h>
#include <sockpool.h>
#include <loop.h>



/* -- C O N S T A N T S -- */
/** @def Project's name */
#define _NAME_      "Tsar"
/** @def Current version */
#define _VERSION_   "0.1Alpha"
/** @def project home */
#define _URL_       "http://code.google.com/p/tsar"

/** @def listen(2) backlog, must be quite important as browsers keep connections alive  */
#define MAX_CONNECTIONS     25

/* -- M A C R O S -- */


/* -- T Y P E D E F S -- */
/** @brief contains the pointers we use to dump the network traffic */
typedef struct {
    /** @brief libpcap handler */
    pcap_t * receiver;
    /** @brief use pcap exported functions to dump network traffic */
    pcap_dumper_t * dumper;
    /** @brief filename where traffic is stored */
    char * dumpFilename;
} sniffer_t;

/** @brief a tsar session contains almost everything required to deal
  with the program's parts */
typedef struct {
    /** @brief pcap dump filename */
    char * dumpFilename;
    /** @brief main HTTP server' socket */
    int webserverSock;
    /** @brief array to store HTTP server' sockets connected to its clients */
    int webserverClients[MAX_CONNECTIONS];
    /** @brief sniffer handle */
    sniffer_t * sniffer;
    /** @brief sockpool handle */
    sockpool_t * pool;
} tsar_t;


/* -- F U N C T I O N S  P R O T O T Y P E S -- */
/**
 * @brief start services according to values contained in the option storage opt_t structure
 * @param o a pointer to an opt_t that contains user or default values to parameters required to start tsar
 * @return an error value according to the execution of the function
 * @retval 0 success case, everything was OK during this session
 * @retval 1 core startup failed
 * @retval 2 sockpool initialization failed
 * @retval 3 event registration failed
 * @retval 4 internal sockpool error occured
 */
int tsar_start_session( opt_t * o );

/**
 * @brief start the different modules of Tsar
 * @param sessions is the address of the tsar session where to store modules handles
 * @param o points to the structure where are stored user's options
 * @return error code according to the execution of the function
 * @retval 0 no error
 * @retval 1 sniffer startupt failed
 * @retval 2 built-in http server startup failed
 */
int tsar_startup( tsar_t * session, opt_t * o );

/**
 * @brief turns the things off
 * @param session is a valid pointer to the session to close
 */
void tsar_shutdown( tsar_t * session );

/**
 * @brief turns on signal handler
 */
void set_signal_handler( void );

/**
 * @brief signal handler's callback that set loop flag to false
 * @param signum let us know which signal has been received
 */
void stop_loop( int signum );

/**
 * @brief print out something like a welcome message
 * @param interface is a null terminated string that contains the name of the network device we use
 */
void print_starting_message( char * interface );

/**
 * @brief register events in sockpool for the sockets we watch
 * @param session contains pointers to the different modules handles
 * @param snifferEnabled describes the sniffer's enabling state, so that we can set the callback or not
 * @return error code according to the execution of the function
 * @retval 0 no error
 */
int tsar_set_callbacks( tsar_t * session );

#endif /* TSAR_H */

